package com.xbjtech.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.xbjtech.mapper.OrderMapper;
import com.xbjtech.model.Order;
import com.xbjtech.model.OrderExample;
import com.xbjtech.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	OrderMapper orderMapper;
	
	@Override
	public List<Order> getOrdersPaged(OrderExample example, int page, int rows) {
		PageHelper.startPage(page, rows);
		return orderMapper.selectByExample(example);
	}

	@Override
	public void insertOrder(Order order) throws Throwable {
		orderMapper.insert(order);
	}

	@Override
	public Order getOrderById(Long id) {
		return orderMapper.selectByPrimaryKey(id);
	}

	// update t_order set sdf=dsaf,dsfaf=asdfa where id =#{value}
	@Override
	public void updateOrder(Order order) throws Exception {
		orderMapper.updateByPrimaryKeySelective(order);
	}

	@Override
	public void deleteOrderById(Long id) throws Exception {
		orderMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Order> getAllOrder(OrderExample example) {
		// 查前1W条记录
		// 确保数量太大的时候，服务器不会宕机
		PageHelper.startPage(1, 10000);
		return orderMapper.selectByExample(example);
	}

	@Override
	public void saveOrders(List<Order> orders) throws Exception {
		for (Order order : orders) {
			orderMapper.insert(order);
		}
	}

}
